package org.example.wax.exception;

import org.example.wax.result.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class ExceptionAdvice {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

    @ExceptionHandler(Exception.class)
    public ResponseEntity handle(Exception e) {
        logger.error(e.getMessage());
        return ResponseEntity.status(HttpServletResponse.SC_OK).body(AjaxResult.error(e.getMessage()));
    }

    @ExceptionHandler(ServiceException.class)
    public ResponseEntity handleServiceException(ServiceException e) {
        logger.error(e.getMessage());
        return ResponseEntity.status(HttpServletResponse.SC_OK).body(AjaxResult.error(e.getMessage()));
    }
}
